package com.checking.attendance.controller;

import com.checking.attendance.pojo.ClockRules;
import com.checking.attendance.pojo.Course;
import com.checking.attendance.service.ClockRulesService;
import com.checking.attendance.util.DateUtil;
import com.checking.attendance.util.ReturnUtil;
import com.checking.attendance.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class ClockRulesJsonController {
    @Autowired
    private ClockRulesService clockRulesService;

    //插入一条考勤规则
    @ResponseBody
    @RequestMapping(value = "/member/clockRulesInsertSelective",method = RequestMethod.POST)
    public ModelMap clockRulesInsertSelective(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {

            if(StringUtils.isEmpty(clockRules.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if (clockRules.getAttendDate() != null){
                clockRules.setAttendDate(clockRules.getAttendDate());
            }

            if (clockRules.getStartAttendTime() != null){
                clockRules.setStartAttendTime(clockRules.getStartAttendTime());
            }

            if (clockRules.getEndAttendTime() != null){
                clockRules.setEndAttendTime(clockRules.getEndAttendTime());
            }

            String id = UuidUtil.getUUID();
            clockRules.setId(id);

            clockRules.setCreateTime(DateUtil.getPresentDate());

            int count = clockRulesService.insertSelective(clockRules);
            if (count == 0){
                return ReturnUtil.error("添加考勤信息失败");
            }

            return ReturnUtil.success("添加考勤信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //查询一条考勤记录并删除
    @ResponseBody
    @RequestMapping(value = "/member/deletClockRulesse",method = RequestMethod.POST)
    public ModelMap deletClockRulesse(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {
            if(StringUtils.isEmpty(clockRules.getId())){
                return ReturnUtil.error("考勤ID不能为空", null, null);
            }

            int count = clockRulesService.deleteByPrimaryKey(clockRules.getId());
            if (count == 0){
                return ReturnUtil.error("删除考勤信息失败");
            }

            return ReturnUtil.success("删除考勤信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //更新一条考勤记录
    @ResponseBody
    @RequestMapping(value = "/member/updateClockRules",method = RequestMethod.POST)
    public ModelMap updateClockRules(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {

            if(StringUtils.isEmpty(clockRules.getId())){
                return ReturnUtil.error("考勤ID不能为空", null, null);
            }

            clockRules.setUpdateTime(DateUtil.getPresentDate());

            int count = clockRulesService.updateByPrimaryKeySelective(clockRules);
            if (count == 0){
                return ReturnUtil.error("修改考勤信息失败");
            }

            return ReturnUtil.success("修改考勤信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //查询一条考勤规则
    @ResponseBody
    @RequestMapping(value = "/member/clockRulesSelect",method = RequestMethod.POST)
    public ModelMap clockRulesSelect(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {
            if(StringUtils.isEmpty(clockRules.getId())){
                return ReturnUtil.error("考勤ID不能为空", null, null);
            }

            ClockRules clockRules1 = clockRulesService.selectByPrimaryKey(clockRules.getId());
            if (clockRules1 == null){
                return ReturnUtil.error("暂无考勤规则信息");
            }

            return ReturnUtil.success("获取考勤信息成功", clockRules1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据用户ID和课程名称查询一条考勤记录规则
    @ResponseBody
    @RequestMapping(value = "/member/seekClockRules",method = RequestMethod.POST)
    public ModelMap seekClockRules(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {

            if(StringUtils.isEmpty(clockRules.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(clockRules.getCourseTitle())){
                return ReturnUtil.error("课程名称不能为空", null, null);
            }

            ClockRules clockRules1 = clockRulesService.seekClockRules(clockRules);
            if (clockRules1 == null){
                return ReturnUtil.error("暂无考勤规则信息");
            }

            return ReturnUtil.success("获取考勤信息成功", clockRules1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据课程名称查询一条考勤规则
    @ResponseBody
    @RequestMapping(value = "/member/queryAttendanceInformation",method = RequestMethod.POST)
    public ModelMap queryAttendanceInformation(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {
            if(StringUtils.isEmpty(clockRules.getCourseTitle())){
                return ReturnUtil.error("课程名称不能为空", null, null);
            }

            ClockRules clockRules1 = clockRulesService.queryClockRulesInformation(clockRules.getCourseTitle());
            if (clockRules1 == null){
                return ReturnUtil.error("暂无考勤规则信息");
            }

            return ReturnUtil.success("获取考勤信息成功", clockRules1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据用户ID和课程名称查询多条记录
    @ResponseBody
    @RequestMapping(value = "/member/queryUserAttendanceInformation",method = RequestMethod.POST)
    public ModelMap queryUserAttendanceInformation(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {
            if(StringUtils.isEmpty(clockRules.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(clockRules.getCourseTitle())){
                return ReturnUtil.error("课程名称不能为空", null, null);
            }

            List<ClockRules> clockRulesList = clockRulesService.queryUserClockRulesInformation(clockRules);
            if (clockRulesList.size() == 0){
                return ReturnUtil.error("暂无考勤规则信息");
            }

            return ReturnUtil.success("获取考勤信息成功", clockRulesList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //查询所有考勤规则
    @ResponseBody
    @RequestMapping(value = "/member/queryAttendanceInformationAll",method = RequestMethod.POST)
    public ModelMap queryAttendanceInformationAll(@Validated @RequestBody ClockRules clockRules, BindingResult result){
        try {

            List<ClockRules> clockRulesList = clockRulesService.queryClockRulesInformationAll();
            if (clockRulesList.size() == 0){
                return ReturnUtil.error("暂无考勤规则信息");
            }

            return ReturnUtil.success("获取考勤信息成功", clockRulesList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }
}
